Skip to main content

Composite types

A composite data type is a collection of similar or different datatypes under a single variable declaration. In other words, it is a data type that has multiple values grouped together.

There a three types of composite data types:

  • Array: An array is a type of object in which multiple data of any data types are stored in a variable. Each value is an array has a numeric position called index.

  • Object: In object data are stored as key-value pairs. But key will be always string and value will be of any data type.

  • Function: Function is a callable object that executes a block of code.

Syntax

// Array
Array()

// Object
Object()

// Function
function name() { // function definition
// function body
}
name() // function call

Work out

var array = [1, 2, 3, "Yellow", "Black"];
var car = {
model: "BMW X3",
color: "black",
type: "racing",
};
function greeting() {
return "Hello world!";
}
console.log(array);
// expected output: [1, 2, 3, "Yellow", "Black"]
console.log(car);
// expected output: { model: "BMW X3", color: "black", type: "racing"}
console.log(greeting());
// expected output: Hello world!
Note

Object, Function and Array are all types of objects. Primitive data types can only store single value at a time, whereas a composite data type can store a collection or complex data.